package com.ruoyi.project.system.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Anonymous;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.SmCustomizedPrice;
import com.ruoyi.project.system.domain.SmProduct;
import com.ruoyi.project.system.domain.vo.ProductVo;
import com.ruoyi.project.system.mapper.SmCustomizedPriceMapper;
import com.ruoyi.project.system.mapper.SmProductMapper;
import com.ruoyi.project.system.mapper.SysUserMapper;
import com.ruoyi.project.system.service.ISmProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 产品Controller
 */
@RestController
@RequestMapping("/system/product")
public class SmProductController extends BaseController {

    @Autowired
    private ISmProductService smProductService;

    @Autowired
    private SmCustomizedPriceMapper smCustomizedPriceMapper;

    @Autowired
    private SmProductMapper smProductMapper;

    @Autowired
    private SysUserMapper userMapper;

    @GetMapping("/noHome")
    public AjaxResult noHomeProducts() {
        return success(smProductMapper.typeProducts(null, "0"));
    }

    @GetMapping("/distinct")
    public AjaxResult distinct() {
        Long userId = SecurityUtils.getUserId();
        AjaxResult result = new AjaxResult();
        result.put("product", smProductMapper.distinctProductName());
        result.put("nick", userMapper.distinctNickName(userId));
        return result;
    }

    /**
     * 获取产品状态
     */
    @Anonymous
    @GetMapping("/status")
    public AjaxResult status(@NotNull Integer productId) {
        AjaxResult result = new AjaxResult();
        SmProduct smProduct = smProductMapper.selectSmProductById(productId);
        result.put("status", smProduct.getStatus());
        result.put("remark", smProduct.getRemark());
        return result;
    }

    /**
     * 自查需求参数
     */
    @GetMapping("/selfQuery")
    public AjaxResult selfQuery(@NotNull Integer productId) {
        AjaxResult result = new AjaxResult();
        SmProduct smProduct = smProductMapper.selectSmProductById(productId);
        result.put("name", smProduct.getName());
        result.put("status", smProduct.getStatus());
        result.put("remark", smProduct.getRemark());

        Long userId = SecurityUtils.getUserId();
        Integer roleId = userMapper.queryRoleId(userId);
        result.put("price", smProductService.basePrice(userId, productId, roleId));
        return result;
    }

    @GetMapping("/setPrice")
    public AjaxResult setPrice(SmCustomizedPrice customizedPrice) {
        Long userId;
        if (customizedPrice.getUserId() == null) {
            userId = SecurityUtils.getUserId();
        } else {
            userId = customizedPrice.getUserId().longValue();
        }
        customizedPrice.setType(userMapper.queryRoleId(userId) + "-" + customizedPrice.getType());
        customizedPrice.setUserId(userId.intValue());
        SmCustomizedPrice smCustomizedPrice = smCustomizedPriceMapper.selectSmCustomizedPrice(customizedPrice);
        if (smCustomizedPrice == null) {
            customizedPrice.setCreateBy(SecurityUtils.getUsername());
            return toAjax(smCustomizedPriceMapper.insertSmCustomizedPrice(customizedPrice));
        } else {
            customizedPrice.setId(smCustomizedPrice.getId());
            customizedPrice.setUpdateBy(SecurityUtils.getUsername());
            return toAjax(smCustomizedPriceMapper.updateSmCustomizedPrice(customizedPrice));
        }
    }

    @GetMapping("/baseInfo")
    public AjaxResult baseInfo(@NotNull Integer productId) {
        Long userId = SecurityUtils.getUserId();
        SmProduct smProduct = smProductService.selectSmProductById(productId);
        ProductVo productVo = new ProductVo(smProduct);
        Integer roleId = userMapper.queryRoleId(userId);
        productVo.setBasePricing(smProductService.basePrice(userId, productId, roleId).setScale(2, BigDecimal.ROUND_DOWN));
        productVo.setPromotionAdd(smProductService.promotionAdd(userId, productId, roleId).setScale(2, BigDecimal.ROUND_DOWN));
        productVo.setBasePriceAdd(smProductService.basePriceAdd(userId, productId, roleId));
        return success(productVo);
    }

    @Anonymous
    @GetMapping("/basePrice")
    public BigDecimal basePrice(@NotNull Long userId, @NotNull Integer productId) {
        Integer roleId = userMapper.queryRoleId(userId);
        return smProductService.basePrice(userId, productId, roleId);
    }

    @Anonymous
    @GetMapping("/promotionPrice")
    public BigDecimal promotionPrice(@NotNull Long userId, @NotNull Integer productId) {
        Integer roleId = userMapper.queryRoleId(userId);
        return smProductService.promotionPrice(userId, productId, roleId);
    }

    @Anonymous
    @GetMapping("/subordinatePrice")
    public BigDecimal subordinatePrice(@NotNull Long userId, @NotNull Integer productId) {
        Integer roleId = userMapper.queryRoleId(userId);
        return smProductService.subordinatePrice(userId, productId, roleId);
    }

    /**
     * 查询产品列表
     */
    @GetMapping("/home")
    public AjaxResult homeList() {
        AjaxResult result = new AjaxResult();
        List<Map<String, String>> PER = smProductMapper.typeProducts("PER", "1");
        List<Map<String, String>> COM = smProductMapper.typeProducts("COM", "1");
        result.put("PER", PER);
        result.put("COM", COM);
        result.put("morePer", smProductMapper.productTypeCount("PER") > PER.size());
        result.put("moreCom", smProductMapper.productTypeCount("COM") > COM.size());
        return result;
    }

    /**
     * 查询产品列表
     */
    @PreAuthorize("@ss.hasPermi('system:product:list')")
    @GetMapping("/list")
    public AjaxResult list() {
        return success(smProductMapper.selectSmProductList_());
    }

    /**
     * 获取产品详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:product:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return success(smProductService.selectSmProductById(id));
    }

    /**
     * 新增产品
     */
    @PreAuthorize("@ss.hasPermi('system:product:add')")
    @Log(title = "产品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SmProduct smProduct) {
        return toAjax(smProductService.insertSmProduct(smProduct));
    }

    /**
     * 修改产品
     */
    @PreAuthorize("@ss.hasPermi('system:product:edit')")
    @Log(title = "产品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SmProduct smProduct) {
        return toAjax(smProductService.updateSmProduct(smProduct));
    }
}
